package com.qianfeng.bob.xiexiaobo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.bob.xiexiaobo.R;
import com.qianfeng.bob.xiexiaobo.bean.QsEntity;

import java.util.List;

/**
 * Created by Administrator on 2015/12/8 0008.
 */
public class QsContentAdapter extends MyBaseAdapter<QsEntity.ItemsEntity> {

    private static final String thumb="http://pic.qiushibaike.com/system/avtnew/%d/%d/thumb/%s";



    public QsContentAdapter(List<QsEntity.ItemsEntity> datas, Context context) {
        super( context,datas);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent, LayoutInflater inflater, List<QsEntity.ItemsEntity> datas) {
        ViewHolder viewHolder;
        if(convertView ==null)
        {
            convertView = inflater.inflate(R.layout.item_content_listview,null);
            viewHolder =new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(datas.get(position).getUser()!=null && datas.get(position).getUser().getLogin()!=null)
        {
            viewHolder.loginName.setText(datas.get(position).getUser().getLogin());
        }
        if(datas.get(position).getContent()!=null) {
            viewHolder.content.setText(datas.get(position).getContent());
        }



        if(datas.get(position).getUser()!=null&&datas.get(position).getUser().getIcon()!=null)
        {
            int id=datas.get(position).getUser().getId();
            viewHolder.thumb.setTag(String.format(thumb, id / 10000, id, datas.get(position).getUser().getIcon()));
             loadImage((viewHolder.thumb));
        }

        return convertView;
    }


    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 90;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }




    class ViewHolder
    {
        private ImageView thumb;
        private TextView loginName,content;
        public ViewHolder(View convertView)
        {
            thumb = ((ImageView) convertView.findViewById(R.id.thumb));
            loginName = ((TextView) convertView.findViewById(R.id.loginName));
            content = ((TextView) convertView.findViewById(R.id.content));
        }
    }



}
